package com.lanou.tipsy.fragment.singer.detail;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.entity.singer.singerList.SingerRank;
import com.lanou.tipsy.fragment.mv.MvFragment;
import com.lanou.tipsy.fragment.person.MineFragment;
import com.lanou.tipsy.fragment.rank.RankFragment;
import com.lanou.tipsy.fragment.recommend.RecommendFragment;
import com.lanou.tipsy.fragment.singer.classify.SingerFragment;
import com.lanou.tipsy.fragment.songsList.SongsListsFragment;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by FengJun on 15/10/22.
 * 歌手->歌手详情->歌曲/专辑/mv/歌手信息
 */
public class SingersInfoFragment extends BaseFragment implements
        RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    public static final String TAG = "SingersInfoFragment";
    private CollapsingToolbarLayout ctl_toolbar;
    private SimpleDraweeView imageView;
    private RadioGroup rg_det_tab;
    private RadioButton rb_det_songs, rb_det_album, rb_det_infos;
    private Toolbar toolbar;
    private SingerRank rank;
    //加载fragment
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private Map<String, SoftReference<ArrayList<Fragment>>> softReferenceMap = new HashMap<>();
    private ArrayList<Fragment> fragments;
    private int choosed = 0;
    private int current = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_info, null);
        if (savedInstanceState != null) {
            current = savedInstanceState.getInt("state");
        }
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());//初始化控件的方法
        Bundle bundle = getArguments();
        rank = (SingerRank) bundle.get("singers");
        String url = rank.getPic_url();
        Uri uri = Uri.parse(url);
        imageView.setImageURI(uri);
        ctl_toolbar.setTitle(rank.getSinger_name());
        //加载fragment集合
        manager = getActivity().getSupportFragmentManager();
        addFragment(TAG);
        getFragmentsByPath(TAG);
    }

    private void initView(View view) {
        imageView = (SimpleDraweeView) view.findViewById(R.id.iv_singers_img);
        ctl_toolbar = (CollapsingToolbarLayout) view.findViewById(R.id.ctl_toolbar_layout);
        rg_det_tab = (RadioGroup) view.findViewById(R.id.rg_det_tab);
        rb_det_songs = (RadioButton) view.findViewById(R.id.rb_det_songs);
        rb_det_album = (RadioButton) view.findViewById(R.id.rb_det_album);
        rb_det_infos = (RadioButton) view.findViewById(R.id.rb_det_infos);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);

        //设置监听
        rg_det_tab.setOnCheckedChangeListener(this);
    }

    //保存软引用到HashMap
    public void addFragment(String path) {
        Log.i("test", TAG + "->添加软引用对象");
        //强引用的对象
        ArrayList<Fragment> datas = new ArrayList<>();
        datas.add(new SDetailSongsFragment(rank.getSinger_id()));
        datas.add(new SDetailAlbumFragment(rank.getSinger_id()));
        datas.add(new SDetailMVFragment(rank.getSinger_id()));
        datas.add(new SDetailInfosFragment(rank.getSinger_id()));
        //软引用fragment集合对象
        SoftReference<ArrayList<Fragment>> softReference = new SoftReference<>(datas);
        //添加该对象到map中使其缓存
        softReferenceMap.put(path, softReference);
    }

    //获取软引用对象
    public ArrayList<Fragment> getFragmentsByPath(String path) {
        Log.i("test", TAG + "->获取软引用对象");
        //从缓存中获取软引用的集合对象
        SoftReference<ArrayList<Fragment>> softReference = softReferenceMap.get(path);
        //判断是否存在软引用
        if (softReference == null) {
            return null;
        }
        //去除集合对象,如果由于内存不足集合对象被回收,将取得空
        fragments = softReference.get();
        return fragments;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_det_songs:
                current = 0;
                break;
            case R.id.rb_det_album:
                current = 1;
                break;
            case R.id.rb_det_mv:
                current = 2;
                break;
            case R.id.rb_det_infos:
                current = 3;
                break;
        }
        if (choosed != current) {
            choosed = current;
            transaction = manager.beginTransaction();
            transaction.replace(R.id.fl_det_tabview, fragments.get(choosed));
            transaction.commit();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("state", current);
    }

    @Override
    public void onResume() {
        super.onResume();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fl_det_tabview, fragments.get(current));
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}
